package app.android.alampang.ui.custom;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageButton;

import app.android.alampang.R;

/**
 * Created by Supanut on 2/12/2015.
 */
public class FloatingActionButton extends ImageButton
{
    private Context context;

    private int color;
    private int colorPressed;

    public FloatingActionButton(Context context) {
        super(context);
        this.context = context;
        init(null);
    }

    public FloatingActionButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs);
    }

    public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void init(AttributeSet attrs)
    {
        Resources.Theme theme = context.getTheme();
        TypedArray arr = theme.obtainStyledAttributes(attrs, R.styleable.FAB, 0, 0);

        try
        {
            setColor(arr.getColor(R.styleable.FAB_bg_color, Color.GRAY));
            setColorPressed(arr.getColor(R.styleable.FAB_bg_color_pressed, Color.BLACK));

            StateListDrawable dStateList = new StateListDrawable();

            dStateList.addState(new int[] {android.R.attr.state_pressed}, createDrawablePressed());
            dStateList.addState(new int[] {}, createDrawable());

            setBackground(dStateList);


            Drawable imgBg = getResources().getDrawable(arr.getResourceId(R.styleable.FAB_bg_img, R.drawable.ic_launcher));
            Rect r = new Rect(0, 0, 0, 0);
            imgBg.setBounds(r);
//            imgBg.setBounds(new Rect());
            setImageDrawable(imgBg);
        }
        catch (Throwable t)
        {

        }
        finally
        {
            arr.recycle();
        }
    }

    private Drawable createDrawable()
    {
        OvalShape oval = new OvalShape();
        ShapeDrawable dOval = new ShapeDrawable(oval);
        setWillNotDraw(false);
        dOval.getPaint().setColor(color);

        Resources resources = context.getResources();
        Drawable dShadow = resources.getDrawable(R.drawable.fab_shape);

        LayerDrawable dLayer = new LayerDrawable(new Drawable[] {  dShadow, dOval });
        dLayer.setLayerInset(0, 0, 0, 0, 0);
        dLayer.setLayerInset(0, 0, 0, 0, 0);

        return dLayer;
    }

    private Drawable createDrawablePressed()
    {
        OvalShape oval = new OvalShape();
        ShapeDrawable dOval = new ShapeDrawable(oval);
        setWillNotDraw(false);
        dOval.getPaint().setColor(colorPressed);

        Resources resources = context.getResources();
        Drawable dShadow = resources.getDrawable(R.drawable.fab_shape_pressed);
        LayerDrawable dLayer = new LayerDrawable(new Drawable[] { dShadow, dOval });
        dLayer.setLayerInset(0, 0, 0, 0, 0);
        dLayer.setLayerInset(0, 0, 0, 0, 0);

        return dLayer;
    }

    private void setColorPressed(int color)
    {
        this.colorPressed = color;
    }

    private void setColor(int color)
    {
        this.color = color;
    }


}
